package com.mmall.security;

import com.mmall.service.SecurityMetadataService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.security.access.ConfigAttribute;
import org.springframework.security.web.FilterInvocation;
import org.springframework.security.web.access.intercept.FilterInvocationSecurityMetadataSource;

import java.util.Collection;
import java.util.Map;

public class MySecurityMetadataSource implements
		FilterInvocationSecurityMetadataSource {

	@Autowired(required = true)
	@Qualifier("securityMetadataService")
	private SecurityMetadataService securityMetadataService;
    private static Map<String, Collection<ConfigAttribute>> local = null;
	@Override
	public Collection<ConfigAttribute> getAllConfigAttributes() {
		return null;
	}

	@Override
	public Collection<ConfigAttribute> getAttributes(Object o)
			throws IllegalArgumentException {
		String url = ((FilterInvocation) o).getRequest().getServletPath();
        Map<String, Collection<ConfigAttribute>> map = null;
		if(local==null)
            { map = securityMetadataService
                .getMetadata();
            local = map;
            }

		return local.get(url.trim());
	}

	@Override
	public boolean supports(Class<?> arg0) {
		return true;
	}

}
